﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.LongER.Business.ZHM001;
using Com.LongER.Framework.Utils;

public partial class ZHM001 : System.Web.UI.Page
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //
        if (!this.Page.IsPostBack)
        {

            Label lblPageId = (Label)Master.FindControl("lblPageId");
            lblPageId.Text = "ZHM001";

            Label lblPageName = (Label)Master.FindControl("lblPageName");
            lblPageName.Text = "用户一览查询画面";

            //清空画面中的输入框
            #region
            this.txt用户ID.Text = string.Empty;
            this.txt姓名.Text = string.Empty;
            //this.list部门.DataBind = string.Empty;
            //this.list职位.Text = string.Empty;

            //总件数
            this.txt总件数.Text = string.Empty;

            //数据表清空
            for (int i = this.tblList.Rows.Count - 1; i >= 1; i--)
            {
                this.tblList.Rows.RemoveAt(i);
            }
            #endregion

            this.setLists();

            //焦点位于用户ID输入框中
            this.txt用户ID.Focus();
        }

    }


    /// <summary>
    /// 点击[执行]按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn执行_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("USERID", this.txt用户ID.Text.ToString());

        //
        if (this.hidPageNo.Value == string.Empty) this.hidPageNo.Value = "1";
        dic.Add("PAGE_NO", this.hidPageNo.Value);


        CZHM001 instance = new CZHM001();


        ZHM001DS.M_USERDataTable dt = instance.GetUSerList(dic);

        #region
        //总件数清空
        this.txt总件数.Text = string.Empty;
        //标题行以外全部移除
        for (int i = this.tblList.Rows.Count - 1; i >= 1; i--)
        {
            this.tblList.Rows.RemoveAt(i);
        }

        Label lblMsgCD = (Label)Master.FindControl("lblMsgCD");
        lblMsgCD.Text = "  ";

        Label lblMsg = (Label)Master.FindControl("lblMsg");
        lblMsg.Text = "  ";
        #endregion


        //查询结果不为空时
        if (dt.Count > 0)
        {
            this.txt总件数.Text = dt.Count.ToString();
        }
        //查询结果为空时
        else
        {
            String msg = MessageUtil.GetMessage("GTH002", "IGTH003001");

            lblMsgCD.Text = "IGTH003001";
            lblMsg.Text = msg;

            return;
        }

        int rowid = 0;
        //
        foreach (var dr in dt)
        {
            //创建新行
            TableRow row = new TableRow();
            if (rowid % 2 == 0) { row.CssClass = "odd"; } else { row.CssClass = "even"; }

            //选择
            TableCell cell = GetTableCell(width: 30, rowspan: 1);
            //
            RadioButton rb = new RadioButton();
            rb.ID = "rb" + rowid;
            rb.GroupName = "rad";
            if (rowid == 0) rb.Checked = true;

            cell.Controls.Add(rb);
            row.Cells.Add(cell);//插入单元格

            //用户ID
            cell = GetTableCell(width: 70);
            cell.Text = dr.USERID;
            row.Cells.Add(cell);

            //用户名
            cell = GetTableCell(width: 70);
            cell.Text = dr.USERID_HANZI;
            row.Cells.Add(cell);

            //部门
            cell = GetTableCell(width: 170);
            cell.Text = dr.BUMEN;
            row.Cells.Add(cell);

            //职位
            cell = GetTableCell(width: 170);
            cell.Text = dr.ZHIWEI;
            row.Cells.Add(cell);

            //联系电话1
            cell = GetTableCell(width: 170);
            cell.Text = dr.MOBIL1;
            row.Cells.Add(cell);

            //联系电话2
            cell = GetTableCell(width: 170);
            cell.Text = dr.MOBIL2;
            row.Cells.Add(cell);

            //邮箱
            cell = GetTableCell(width: 170);
            cell.Text = dr.MAIL_ADDR;
            row.Cells.Add(cell);
            
            this.tblList.Rows.Add(row);


            rowid++;

        }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="width"></param>
    /// <param name="rowspan"></param>
    /// <returns></returns>
    private TableCell GetTableCell(int width = -1, int rowspan = 1, int colspan = 1)
    {
        //创建单元格对象
        TableCell cell = new TableCell();
        if (width != -1) cell.Width = width;
        if (rowspan > 1) cell.RowSpan = rowspan;
        if (colspan > 1) cell.ColumnSpan = colspan;
        return cell;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn主菜单_Click(object sender, EventArgs e)
    {
        Response.Redirect("/LongER/ZAH003.aspx");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private Boolean setLists()
    {
        CZHM001 instance = new CZHM001();
        ZHM001DS.M_TONGYONGDataTable ds = instance.GetLists();

        var query = from data in ds
                    where data.CODE_CD == "BUMN"
                    select data;

        this.list部门.DataSource = query;
        this.list部门.DataTextField = "CODE1_HANZI";
        this.list部门.DataValueField = "KEY1";
        this.list部门.DataBind();
        this.list部门.SelectedIndex = 0;

        query = from data in ds
                where data.CODE_CD == "ZHIW"
                select data;

        this.list职位.DataSource = query;
        this.list职位.DataTextField = "CODE1_HANZI";
        this.list职位.DataValueField = "KEY1";
        this.list职位.DataBind();
        this.list职位.SelectedIndex = 0;

        return true;
    }

}